package com.dtx.datastruct.array;

public class Array {
    private int maxSize;
    private Entity[] array;
    private int size;

    public Array(int maxSize) {
        this.maxSize = maxSize;
        this.size=0;
        array = new Entity[maxSize];
    }

    private boolean isFull(){
        return size==maxSize;
    }

    private boolean isEmpty(){
      return size==0;
    }

    public void add(Entity entity){
        if (isFull()) {
            System.out.println("数组为满,无法添加");
            return;
        }
        array[size] = entity;
        size++;
    }

    public void list(){
        for (int i = 0; i < size; i++) {
            System.out.println(array[i]);
        }
    }

    public void insert(Entity entity,int index){
        if (isFull()) {
            System.out.println("数组已满，无法插入");
            return;
        }
        if(index<0 || index>maxSize){
            System.out.println("插入位置有错误，请重新输入");
            return;
        }
        for (int i = size - 1;i>=index-1;i--){
            array[i+1] = array[i];
        }
        array[index-1] = entity;
        size++;
    }

    public Entity del(int index){
        if (isEmpty()) {
            throw new RuntimeException("数组为空，删除没意义");
        }
        if(index<0 || index>maxSize){
            throw new RuntimeException("删除位置有错误，请重新输入");
        }
        for (int i = index-1; i < size - 1; i++) {
            array[i] = array[i+1];
        }
        size--;
        return array[index-1];
    }

    public Entity[] getArray() {
        return array;
    }

    public void setArray(Entity[] array) {
        this.array = array;
    }


}
